<?php
// $Id$

/**
 * Controller_Admin_Agent 控制器
 */
class Controller_Admin_Agent extends Controller_Abstract
{
	
	function _format_appendSql()
	{
		if (isset($_SESSION['acct']))
		{
			if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
			{
			//为管理
				//$append['']
				$append['admin_id'] = $_SESSION['acct']['id'];
				
			}else
			{
				$append['admin_id'] = $_SESSION['acct']['admin_id'];
				$append['id'] = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
		
			}
		}
		return $append;
	}
	function actionmyrate()
	{
		
	}
	/**
	 * 短信开关
	 */
	function actionsmsflag()
	{
		$ids = $this->_context->ids;
		//$param = $this->_format_appendSql();
		foreach ($ids as $k=>$v)
		{
			
				$personIDS['id'] = Acct::_get_have_acctid($v, $_SESSION['acct']['id']);
				$person = Acct::find($personIDS)->getAll();
				foreach($person as $i=>$ii)
				{
					$ii->smsflag = $ii->smsflag ? 0 : 1;
					$ii->save(0,'update');
				}
		}
		return $this->success('操作成功');
	}
	function actiondel()
	{
		$ids = $this->_context->ids;
		$param = $this->_format_appendSql();
		foreach ($ids as $k=>$v)
		{
			
			$acct = Acct::find('id = ?',$v)->getOne();
			Acct::rm($acct->id, $acct->admin_id);
		}
		Syslog::logs('删除了ID:'.print_r($ids,true).'的代理');
		$this->union_table = Acct::meta();
		$this->delete(array_merge($this->_format_appendSql(),array('id'=>$this->_context->ids)));
		
	}
	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		//$this->page_size = 5;
		$this->union_table = acct::meta();
		if ($this->_context->agent_id)
		{
			$has_subid = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
			
			if (in_array($this->_context->agent_id,$has_subid))
			{
				$this->lists(array('id'=>$has_subid,'admin_id'=>intval($this->_context->admin_id),'agent_id'=>intval($this->_context->agent_id)));				
			}else
			{
				exit('权限不够');
			}
		}else
		{
			if ($this->_context->acctname)
			{
			$this->lists(parent::_format_appendSql());//array('admin_id'=>-1)为管理
			}else {
			$this->lists(array('agent_id'=>$_SESSION['acct']['id']));		
			}
		}
		
	}
	function actionadd()
	{
		if($_SESSION['acct']['agents_have'] == 0 && $_SESSION['acct']['acclevel'] != 0)
		{
			return $this->error('对不起,您没有权限开通代理');
		}
		$this->union_table = Acct::meta();
		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_acct_add(url('agent/add'));
		 if ($this->_context->isPOST())
		 {
		 	if (isset($_POST['id']))
		 	{
// 		 		$isone = Acct::find('id <> ? and agent_prefix = ?',$this->_context->id,$this->_context->agent_prefix)->getCount('id');
// 		 		if($isone)
// 		 		{
// 		 			return $this->error('代理前缀已经存在,请更换！');
// 		 		}else
		 		if ($_POST['id'] == $_SESSION['acct']['id'])
		 		{
		 			return $this->error('您不能修改自己');
		 		}
		 		if(!($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1))
		 		{
		 			$_POST['batuserbalance'] = 1;
		 		}
		 		//不允许修改admin_id agent_id
		 		/*if (isset($_POST['admin_id']))
		 		{
		 		unset($_POST['admin_id']);
		 		}
		 		if (isset($_POST['agent_id']))
		 		{
		 		unset($_POST['agent_id']);
		 		}*/
		 		
		 	}
		 	 if (intval($this->_context->id) == 0){
		 		$isacctname = Acct::find('acctname = ?',$this->_context->acctname)->getCount('id');
		 		if ($isacctname > 0)
		 		{
		 			return $this->error('存在此帐号');
		 		}
		 	}
		 }
		
		 $result = $handle->add($form, 'id', $_GET);
		 if ($this->_context->isGET())
		 {		
		 		/*if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
		 		{
		 		$result->element('admin_id')->value = Acct::cur_admin_id();
		 		$result->element('agent_id')->value = $_SESSION['acct']['id'];
		 		}else{
		 		$result->element('admin_id')->value = Acct::cur_admin_id();
		 		$result->element('agent_id')->value = $_SESSION['acct']['id'];
		 		}*/
		 		$group = isset($_GET['id']) ? Rategroup::_get_arrayinfo(array('agent_id'=>$_SESSION['acct']['id'])) : Rategroup::_get_agentArray(false,0) ;
		 		$result->element('rategroupid')->items =  $group;
		 		$result->element('rategroupid_acall')->items = $group;
		 		$result->element('smsurl')->value = Acct::getAdminSmsurl();
		 		$result->element('gatewaygroupid')->items = Getewaygroup::_get_arrayinfo('gatewaygroupid');
		 		$result->element('gatewaygroupid_acall')->items = Getewaygroup::_get_arrayinfo('gatewaygroupid_acall');
		 		$this->_view['form'] = $result;
		 		$this->_viewname = '../form/add';		
		 }else {
		 		Syslog::logs('修改代理:'.$_POST['acctname']);
		 		
		 		return call_user_func_array(array($this,$handle->status),array($result));
		 }
	}
	/**
	 * 转账费率设置
	 */
	function actiontransfer()
	{
		if($this->_context->isPOST())
		{
			$transfer = floatval($this->_context->transfer);
			if(!QValidator::validate_between($transfer, 0, 100))
			{
				return $this->error('比例必须在0-100%以内');
			}
			$acct = acct::find('id = ? and acctname = ?',$_SESSION['acct']['id'],$_SESSION['acct']['acctname'])->getone();
			if(!$acct->unionset->id)
			{
				$union = new Acctextension();
				$union->acctid = $acct->id;
				$union->transfer = $transfer;
				$union->save(0,'create');
			}else{
				$union = $acct->unionset;
				$union->transfer = $transfer;
				$union->save(0,'update');
			}
			return $this->success('设置成功');
		}
	}
	/**
	 * 直接设置代理余额
	 */
	function actionsetbalance()
	{
		$set = Q::ini('appini/agentcard');
		#增加运营
		//$acct = Acct::find('id = ?',$_SESSION['acct']['id'])->getOne();
		
		if (!array_key_exists($_SESSION['acct']['acctname'], $set))
		{
			exit('须运营人员权限');
		}
		$balance = intval($this->_context->balance);
		$acctname = ($this->_context->name);
		$acct = Acct::find('acctname = ? ',$acctname)->getOne();
		if (!$acct->id()){
			exit('不存在代理');
		}else{
			$old = $acct->balance;
			$acct->balance = $balance;
			Syslog::logs($_SESSION['acct']['acctname'].' 成功设置 '.$acct->acctname.' 的余额为：'.$balance.',原始:'.$old);				
			$acct->save(0,'update');
			exit($acct->acctname." 的余额已经修改为:".$balance);
		}
	}
	/**
	 * 代理WAP设置
	 */
	function actionwapset()
	{
		
		if ($this->_context->isGET())
		{
			$group = isset($_GET['id']) ? Rategroup::_get_arrayinfo(array('agent_id'=>$_SESSION['acct']['id'])) : Rategroup::_get_agentArray(false,0) ;
			$count = Acctwapset::find('acctid = ? ',$_SESSION['acct']['id'])->getCount('acctid');
			
			if (intval($count) == 0)
			{
				$first = array_keys($group);
				if (intval($first[0]))
				{
					$data = new Acctwapset();
					$data->acctid = $_SESSION['acct']['id'];
					
					$data->rategroupid =  $first[0];
					$data->rategroupid_acall = $first[0];
					
					$data->save(0,'create');
				}else{
					exit('请先添加费率');
				}
				
			}else{
				$data = Acctwapset::find('acctid = ? ',$_SESSION['acct']['id'])->asArray()->getOne();
			}
			$_GET['acctid'] = $_SESSION['acct']['id'];
			$form = new Form_acct_wapset(url('agent/wapset'));
			$form->import($data);
			$form->element('rategroupid')->items =  $group;
			$form->element('rategroupid_acall')->items = $group;
				
			$this->_view['form'] = $form;
			$this->_viewname = '../form/add';
		}else{
			$data = Acctwapset::find('acctid = ? ',$_SESSION['acct']['id'])->getOne();
			$data->systitle_url = trim($this->_context->systitle_url);
			$data->shop_url = trim($this->_context->shop_url);
			$data->hotcall_url = trim($this->_context->hotcall_url);
			$data->download_url = trim($this->_context->download_url);
			$data->productdomain = trim($this->_context->productdomain);
			
			$data->rategroupid = intval($this->_context->rategroupid);
			$data->rategroupid_acall = intval($this->_context->rategroupid_acall);
			$data->safe_money = floatval($this->_context->safe_money);
			$data->open_reg = intval($this->_context->open_reg);
			$data->expireddays = intval($this->_context->expireddays);
			$data->acct_scoreset = floatval($this->_context->acct_scoreset);
			$data->user_scoreset = floatval($this->_context->user_scoreset);
				
			$data->e_appid = ($this->_context->e_appid);
			$data->e_appkey = ($this->_context->e_appkey);
			$data->appname = trim($this->_context->appname);
			$data->appimg = trim($this->_context->appimg);
				
			$data->smsmsg = trim($this->_context->smsmsg);
			$data->wxname = trim($this->_context->wxname);
			$data->wxtoken = trim($this->_context->wxtoken);
			
			$data->save();
				
			Syslog::logs('修改代理WAP设置:'.print_r($_POST,true));
			$this->success('修改成功');
		}
	}
	function actionwapnewsAdd()
	{
		$this->union_table = Acctwapnews::meta();
		$handle = new helper_autoinsert($this->union_table);
		$form = new Form_acct_wapnewsadd(url($this->_context->controller_name.'/'.$this->_context->action_name));
		$result = $handle->add($form, 'id',$_GET);
		
		
		if ($this->_context->isGET())
		{
			//添加 编辑时候 默认所属代理
			$result->element('acctid')->value = $_SESSION['acct']['id'];
			$this->_view['form'] = $result;
			$this->_viewname = '../form/add';
		}else {
			Syslog::logs('添加WAP新闻:'.$_POST['title']);
			return call_user_func_array(array($this,$handle->status),array($result));
		}
	}
	
	function actionnewsindex()
	{
		$this->union_table = Acctwapnews::meta();
		//$agent_list = Acct::_get_arrayinfo();
		//$this->_view['agent_list'] = $agent_list;
		$this->lists(array('acctid'=>$_SESSION['acct']['id']));//array('admin_id'=>-1)为管理
	}
	function actionwapnewsdel()
	{
		Syslog::logs('删除WAP新闻:'.$this->_context->ids);
		$this->union_table = Acctwapnews::meta();
		$this->delete(array_merge(array('acctid'=>$_SESSION['acct']['id']),array('id'=>$this->_context->ids)));
	}
	function actionscorelist()
	{
		$this->union_table = Wapscorelog::meta();
		$param = $this->_format_appendSql();
		if(isset($param['id']))
		{
			$param['acct_id'] = $param['id'];
			$param['acct_id'][] = $_SESSION['acct']['id'];
			unset($param['id']);
		}
		//$param['type'] = 1;
		$this->lists($param);
	}
}


